package com.luoying.itew.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.luoying.itew.R;
import com.luoying.itew.bean.CollectorBean;
import com.luoying.itew.common.Constant;
import com.luoying.itew.util.MonitorUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by lufan on 2018/5/28
 * Class describe 采集器适配器
 */
public class CollectAdapter extends RecyclerView.Adapter<CollectAdapter.CollectViewHolder> {
    private List<CollectorBean> list;
    private Context context;
    private CollectOnClickListener listener;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());


    public CollectAdapter(List<CollectorBean> list ,Context context) {
        this.list = list;
        this.context = context;
    }

    public void setListener(CollectOnClickListener listener){
        this.listener = listener;
    }

    @Override
    public CollectViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_collector, parent, false);
        return new CollectViewHolder(view);
    }

    @Override
    public void onBindViewHolder(CollectViewHolder holder, final int position) {
        CollectorBean bean = list.get(position);
        if(bean!=null){
            holder.nameTv.setText(bean.getCollectorName());
            int state = MonitorUtils.CollectorLevel(bean);
            if(bean.getMonitorPointId()==100000003){
                state = Constant.WARN_LEVEL_GREEN;
            }
            switch (state){
                default:
                case Constant.WARN_LEVEL_GREEN:
                    holder.stateTv.setText(holder.rootView.getResources()
                            .getString(R.string.warn_leve_green));
                    holder.stateTv.setTextColor(context.getResources().getColor(R.color.green_level));
                    break;
                case Constant.WARN_LEVEL_YELLOW:
                    holder.stateTv.setText(holder.rootView.getResources()
                            .getString(R.string.warn_leve_yellow));
                    holder.stateTv.setTextColor(context.getResources().getColor(R.color.yellow_level));
                    break;
                case Constant.WARN_LEVEL_ORANGE:
                    holder.stateTv.setText(holder.rootView.getResources()
                            .getString(R.string.warn_leve_orange));
                    holder.stateTv.setTextColor(context.getResources().getColor(R.color.orange_level));
                    break;
                case Constant.WARN_LEVEL_RED:
                    holder.stateTv.setText(holder.rootView.getResources()
                            .getString(R.string.warn_leve_red));
                    holder.stateTv.setTextColor(context.getResources().getColor(R.color.red_level));
                    break;
            }
            if(!TextUtils.isEmpty(bean.getWarnTime())){
                try {
                    holder.timeTv.setText("报警时间:"+simpleDateFormat.format(new Date(Long.parseLong(bean.getWarnTime()))));
                    holder.timeTv.setVisibility(View.VISIBLE);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            } else {
                holder.timeTv.setVisibility(View.GONE);
            }
            holder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener!=null){
                        listener.onClick(list.get(position));
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class CollectViewHolder extends RecyclerView.ViewHolder {
        TextView nameTv;
        TextView stateTv;
        TextView timeTv;
        View rootView;
        public CollectViewHolder(View itemView) {
            super(itemView);
            rootView = itemView;
            nameTv = (TextView) itemView.findViewById(R.id.collector_name);
            stateTv = (TextView) itemView.findViewById(R.id.collector_state);
            timeTv = (TextView) itemView.findViewById(R.id.collector_time);
        }
    }

    public interface CollectOnClickListener{
        void onClick(CollectorBean bean);
    }

}
